﻿using Engine;
using ThinkDotNetEditorLib.Commands;
using ThinkDotNetEditorLib.Dialogs;
using ThinkDotNetEditorLib.Services.DialogWindowService;

namespace ThinkDotNetEditorLib.Controllers
{
    public class ShellController:ControllerBase
    {
        private readonly IShellForm ShellForm;
        private const string MapFilter = "Map Files (*.map)|*.map";

        #region Constructors

        public ShellController(IShellForm shellForm)
        {
            ShellForm = shellForm;
        }

        #endregion

        #region Properties

        public bool CanUndo { get { return CommandManager.Instance.CanUndo; } }
        public bool CanRedo { get { return CommandManager.Instance.CanRedo; } }
        public string CurrentDocumentPath { get; private set; }
	    
        #endregion

        #region Methods

        public void RemoveSelectedObjects()
        {
            var arenaController = ServiceLocator.Instance.Resolve<IArenaController>();
            arenaController.RemoveSelectedObjects();
        }
        public void Open()
        {
            string path = DialogWindowService.ShowOpenFileDialog(MapFilter);
            if (string.IsNullOrEmpty(path) == false)
            {
                IWorldLoader worldLoader = ServiceLocator.Instance.Resolve<IWorldLoader>();
                IGameEditor gameEditor = ServiceLocator.Instance.Resolve<IGameEditor>();
                gameEditor.World=worldLoader.Load(gameEditor.XnaGame, path);
                CurrentDocumentPath = path;
                CommandManager.Instance.Clear();
                ShellForm.RefreshUndoRedoIsEnabled();
            } 
        }
        public void Save()
        {
            if (string.IsNullOrEmpty(CurrentDocumentPath))
                SaveAs();
            else
                SaveAs(CurrentDocumentPath);

        }
        public void ShowAboutBox()
        {
            var dialogService = ServiceLocator.Instance.Resolve<IDialogWindowService>();
            dialogService.ShowDialog(ShellForm, new AboutBox());
        }
        public void SaveAs(string path)
        {
            IWorldLoader worldLoader = ServiceLocator.Instance.Resolve<IWorldLoader>();
            IGameEditor gameEditor = ServiceLocator.Instance.Resolve<IGameEditor>();
            worldLoader.Save(gameEditor.World, path);
            CurrentDocumentPath = path;
        }
        public void SaveAs()
        {
            string path = DialogWindowService.ShowSaveFileDialog(MapFilter);
            if(string.IsNullOrEmpty(path)==false)
            {
               SaveAs(path);
            }
        }
        public void Exit()
        {
            System.Windows.Forms.Application.Exit();
        }
        public void Undo()
        {
            CommandManager.Instance.Undo();
            ShellForm.RefreshUndoRedoIsEnabled();
        }
        public void Redo()
        {
            CommandManager.Instance.Redo();
            ShellForm.RefreshUndoRedoIsEnabled();
        }
        
        #endregion       
    }
}
